package cn.yunhe.action;

import cn.yunhe.entity.User;
import cn.yunhe.service.UserService;
import cn.yunhe.util.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import lombok.Data;

import java.io.IOException;
import java.io.PrintWriter;

@Data
public class UserAction extends BaseAction implements ModelDriven {
    private User user;
    private PrintWriter out;
    private UserService userService;

    /**
     * 模型驱动
     *
     * @return
     */
    @Override
    public Object getModel() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    /**
     * 用户注册
     *
     * @return
     */
    public String regUser() {
        userService = new UserService();
        if (userService.regUser(user) > 0) {
            return SUCCESS;
        } else {
            return INPUT;
        }
    }

    /**
     * 用户登录
     *
     * @return
     */
    public String userLogin() {
        userService = new UserService();
        user = new User(user.getName(), user.getPassword());

        if (userService.userLogin(user) != null) {
            return SUCCESS;
        } else {
            return INPUT;
        }
    }

    /**
     * 用户是否是管理员
     *
     * @return
     */
    public String userIsAdmin() {
        userService = new UserService();
        User u = userService.userIsAdmin(user.getName());
        if (u != null) {
            return SUCCESS;
        } else {
            return INPUT;
        }
    }

    /**
     * 通过用户名，查询是否存在
     * @param
     * @return
     */

    public String getAllUserByName() throws IOException {
        userService = new UserService();
        boolean flag = userService.getAllUserByName(user.getName());
        out = getResponse().getWriter();
        if (flag){
            out.print("true");
        }else {
            out.print("false");
        }
        return null;
    }




}
